package com.tuanzi.loan.business.repository.system.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanSchedule.LOAN_SCHEDULE;

import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.tuanzi.loan.business.repository.system.custom.ScheduleRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.system.ScheduleSearchRequest;
import com.tuanzi.loan.vo.response.system.ScheduleSearchResponse;

/**
 * @author Ray 2017/09/21
 */
@SuppressWarnings({ "rawtypes" })
public class ScheduleRepositoryImpl extends JOOQBaseRepository implements ScheduleRepositoryCustom {

	@Override
	protected TableImpl getTable() {
		return LOAN_SCHEDULE;
	}

	@Override
	public Page<ScheduleSearchResponse> list(ScheduleSearchRequest searchRequest, Pageable pageable) {
		contains(LOAN_SCHEDULE.TASK_NAME, searchRequest.getTaskName());
		eq(LOAN_SCHEDULE.TASK_GROUP, searchRequest.getTaskGroup());
		eq(LOAN_SCHEDULE.EXECUTE_TYPE, searchRequest.getExecuteType());
		eq(LOAN_SCHEDULE.START_TYPE, searchRequest.getStartType());
		eq(LOAN_SCHEDULE.ENABLE, searchRequest.getEnable());
		return search(pageable, new ScheduleSearchResponseRecordMapper());
	}

	private final class ScheduleSearchResponseRecordMapper implements RecordMapper<Record, ScheduleSearchResponse> {

		@Override
		public ScheduleSearchResponse map(Record record) {
			ScheduleSearchResponse response = new ScheduleSearchResponse();
			response.setCron(record.get(LOAN_SCHEDULE.CRON));
			response.setTaskName(record.get(LOAN_SCHEDULE.TASK_NAME));
			response.setTaskGroup(record.get(LOAN_SCHEDULE.TASK_GROUP));
			response.setExecuteType(record.get(LOAN_SCHEDULE.EXECUTE_TYPE));
			response.setStartType(record.get(LOAN_SCHEDULE.START_TYPE));
			response.setId(record.get(LOAN_SCHEDULE.ID));
			response.setEnable(record.get(LOAN_SCHEDULE.ENABLE));
			return response;
		}
	}
}
